const fs = require("fs");
const stream = require("stream");
const path = require("path");
const util = require("util");

function MhtStream(source){
    stream.Readable.call(this);
    this._source = source;
    this._foundLineEnd = false;
    this._buffer = "";

    source.on("readable",function(){
        this.read();
    }.bind(this))

    source.on("data",function(d){
        this.read()
    }.bind(this))
}

util.inherits(MhtStream,stream.Readable);

MhtStream.prototype._read = function(size){
    var chunk;
    var line;
    var lineIndex;
    var result;
    if(this._buffer.length==0){
        chunk = this._source.read();
        this._buffer +=chunk;
    }
    // debugger
    lineIndex = this._buffer.indexOf("\n");

    if(lineIndex!==-1){
        line = this._buffer.slice(0,lineIndex);
        if(line){
            debugger
            this._buffer = this._buffer.slice(lineIndex+1);
            this.push(`<<${line}>>`)
        }else{
            this._buffer = this._buffer.slice(1);
        }
    }
}

let input = new MhtStream(fs.createReadStream(
    path.join(__dirname,"./bigger.mht"),
    {
        encoding:"utf8"
    }
))